package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

//一定要添加注解@WebServlet("login"),才能正常运行servlet
@WebServlet("/login")

//loginServlet这个类继承HttpServlet
public class loginServlet extends HttpServlet {

	//重写doGet方法
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			this.doPost(req, resp);
	}

	//重写doPost方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	//设置请求的编码方式为utf-8
	req.setCharacterEncoding("utf-8");
	//设置响应的内容类型为 text/html;charset = utf-8
	resp.setContentType("text/html;charset=utf-8");
	//获取传递过来的参数username
	String name = req.getParameter("username");
	//获取传递过来的参数pwd
	String password = req.getParameter("pwd");
	//使用out输出流
	PrintWriter out = resp.getWriter();
	//如果用户名为tiger并且密码为123,跳转到登录成功的界面
	if(name.equals("tiger") && password.equals("123")) {
out.println("<html>");
out.println("<head><title>登录成功的界面</title></head>");
out.println("<body bgcolor='pink'>");
out.println("<h1>用户"+name+"你好,欢迎您登录学生信息管理系统</h1><hr>");
out.println("<table><tr><td>学生姓名</td><td>学生学号</td><td>学生宿舍号</td><td>学生手机号</td></tr>");
out.println("<tr><td>小明</td><td>001</td><td>3-423</td><td>158898989</td></tr>");
out.println("<tr><td>小强</td><td>002</td><td>3-424</td><td>158898444</td></tr>");
out.println("<tr><td>小红</td><td>003</td><td>3-425</td><td>158898555</td></tr>");
out.println("</table></body></html>");
	}else {
			//如果用户名不是tiger或者密码不是123,跳转到登录失败的界面
			out.println("<html>");
			out.println("<head><title>登录失败的界面</title></head>");
			out.println("<body bgcolor='orange'");
			out.println("<h1>对不起,用户名或密码错误,请重新输入!");
			out.println("<a href='login.html'>重新登录</a></h1>");
			out.println("</body></html>");
		}	
			//关闭输出流
			out.close();
	}	
}
